home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / PAT.C < prev    next >
C/C++ Source or Header  |  1993-02-09  |  6KB  |  98 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 02-04-93 (07:15)             Number: 225
  4. From: BOB STOUT                    Refer#: NONE
  5.   To: JOSH EDLER                    Recvd: NO  
  6. Subj: wildcard string compare        Conf: (36) C Language
  7. ---------------------------------------------------------------------------
  8. In a message of <Feb 01 16:37>, Josh Edler (1:2270/233.36@fidonet) writes:
  9.  
  10.  >I need a procedure to compare two strings, one with a wildcard, and one
  11.  >normal.  The wildcard would be a DOS file mask, and the other would be a
  12.  >filename.
  13.  
  14.   From SNIPPETS:
  15.  
  16. #include <stddef.h>
  17. #include <string.h>
  18.  
  19. /***********************************************************************
  20. **                                                                    **
  21. **   Function    :  patmat                                            **
  22. **                                                                    **
  23. **   Purpose     :  Pattern Matching                                  **
  24. **                                                                    **
  25. **   Usage       :  Pass two string pointers as parameters.The first  **
  26. **                  being a raw string & the second a pattern the raw **
  27. **                  string is to be matched against.If the raw string **
  28. **                  matches the pattern,then the function returns a   **
  29. **                  1,else it returns a 0.                            **
  30. **                                                                    **
  31. **                  e.g  patmat("abcdefghi","*ghi") returns a 1.      **
  32. **                       patmat("abcdefghi","??c??f*") returns a 1.   **
  33. **                       patmat("abcdefghi","*dh*") returns a 0.      **
  34. **                       patmat("abcdefghi","*def") returns a 0.      **
  35. **                                                                    **
  36. **                  The asterisk is a wild card to allow any charac-  **
  37. **                  ters from its first appearance to the next spec-  **
  38. **                  ific character.The character ? is a wild card     **
  39. **                  for only one character in the position it appears.**
  40. **                  Combinations such as "*?" or "?*" or "**" are     **
  41. **                  illegal for obvious reasons & the functions may   **
  42. **                  goof,though I think it will still work.           **
  43. **                                                                    **
  44. **   Author      :  Sreenath Chary              Nov 29 1988           **
  45. **                                                                    **
  46. **   Logic       :  The only simple way I could devise is to use      **
  47. **                  recursion.Each character in the pattern is        **
  48. **                  taken & compared with the character in the raw    **
  49. **                  string.If it matches then the remaining amount    **
  50. **                  of the string & the remaining amount of the       **
  51. **                  pattern are passed as parameters to patmat again  **
  52. **                  until the end of the pattern.If at any stage      **
  53. **                  the pattern does not match,then we go back one    **
  54. **                  level & at this level if the previous character   **
  55. **                  was a asterisk in the pattern,we hunt again from  **
  56. **                  where we left off,otherwise we return back one    **
  57. **                  more level with a not found & the process goes    **
  58. **                  on till the first level call.                     **
  59. **                                                                    **
  60. **                  Only one character at a time is considered,except **
  61. **                  when the character is an asterisk.You'll get the  **
  62. **                  logic as the program unfolds.                     **
  63. **                                                                    **
  64. ***********************************************************************/
  65.  
  66. patmat(char *raw,char *pat)
  67. {  int  i, slraw;
  68.  
  69.    if ((*pat == '\0') && (*raw == '\0'))    /*  if it is end of both  */
  70.      return( 1 ) ;                          /*  strings,then match    */
  71.    if (*pat == '\0')                        /*  if it is end of only  */
  72.      return( 0 ) ;                          /*  pat tehn mismatch     */
  73.    if (*pat == '*')                         /* if pattern is a '*'    */
  74.     { if (*(pat+1) == '\0')                 /*    if it is end of pat */
  75.          return( 1 ) ;                      /*    then match          */
  76.       for(i=0,slraw=strlen(raw);i<=slraw;i++)/*    else hunt for match*/
  77.         if ((*(raw+i) == *(pat+1)) ||       /*         or wild card   */
  78.             (*(pat+1) == '?'))
  79.          if (patmat(raw+i+1,pat+2) == 1)    /*      if found,match    */
  80.               return( 1 ) ;                 /*        rest of pat     */
  81.     }
  82.    else
  83.     { if (*raw == '\0')                     /*  if end of raw then    */
  84.          return( 0 ) ;                      /*     mismatch           */
  85.       if ((*pat == '?') || (*pat == *raw))  /*  if chars match then   */
  86.         if (patmat(raw+1,pat+1) == 1)       /*  try & match rest of it*/
  87.            return( 1 ) ;
  88.     }
  89.    return( 0 ) ;                            /*  no match found        */
  90. }
  91.  
  92.  
  93. --- QM v1.00
  94.  * Origin: MicroFirm : Down to the C in chips (1:106/2000.6)
  95. SEEN-BY: 1/211 11/1 2 4 13/13 101/1 108/89 109/25 110/69 114/5 123/19
  96. SEEN-BY: 124/1 153/752 154/40 77 157/2 159/100 125 430 575 950 203/23
  97. SEEN-BY: 209/209 280/1 390/1 396/1 5 15 730/6 2270/1 3603/20
  98.